package com.right.near.business.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.right.near.business.service.IUserService;
import lombok.AllArgsConstructor;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.right.near.business.model.ao.UserPageAO;
import com.right.near.business.model.vo.UserVO;
import com.right.near.business.model.ao.UserInfoAO;
import com.right.near.business.model.vo.UserInfoVO;
import com.right.near.business.model.ao.UserAddAO;
import com.right.near.business.model.vo.UserAddVO;
import com.right.near.business.model.ao.UserUpdateAO;
import com.right.near.business.model.ao.UserDeleteAO;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.List;

/**
 * 用户接口
 *
 * @author star
 * @since 2022-07-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class UserController {

    private final IUserService service;


    /**
     * 用户分页查询
     *
     * @param ao 请求参数
     * @return Page<UserVO> 响应参数
     */
    @PostMapping("/page")
    public Page<UserVO> queryUserPage(@Validated @RequestBody UserPageAO ao) {
        return service.queryUserPage(ao);
    }

    /**
     * 用户明细查询
     *
     * @param ao 请求参数
     * @return UserInfoVO 响应参数
     */
    @GetMapping("/detail")
    public UserInfoVO queryUserDetail(@Validated @RequestBody UserInfoAO ao) {
        return service.queryUserDetail(ao);
    }

    /**
     * 用户新增数据
     *
     * @param ao 请求参数
     * @return UserAddVO 响应参数
     */
    @PostMapping("/add")
    public UserAddVO addUser(@Validated @RequestBody UserAddAO ao) {
        return service.addUser(ao);
    }

    /**
     * 用户修改数据
     *
     * @param ao 数据
     */
    @PutMapping("/update")
    public void updateUser(@Validated @RequestBody UserUpdateAO ao) {
        service.updateUser(ao);
    }

    /**
     * 用户删除数据
     *
     * @param ao 请求参数
     */
    @DeleteMapping("/delete")
    public void deleteUser(@Validated @RequestBody UserDeleteAO ao) {
        service.deleteUser(ao);
    }

    /**
     * 用户批量删除数据
     *
     * @param aos 请求参数
     */
    @DeleteMapping("/batch/delete")
    public void deleteBatchUser(@Validated @RequestBody @NotNull(message = "删除数据不能为空") List<UserDeleteAO> aos) {
        service.deleteBatchUser(aos);
    }

    /**
     * 用户导出数据
     *
     * @param ao 请求参数
     * @param response 响应流
     */
    @GetMapping("/export")
    public void exportUser(UserPageAO ao, HttpServletResponse response) throws IOException {
        StringBuilder fileName = new StringBuilder();
        fileName.append("用户-").append(LocalDate.now()).append(".xlsx");
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName.toString(), "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        service.exportUser(ao, outputStream);
        IOUtils.closeQuietly(outputStream);
    }
}
